import { Vector2, Vector3 } from "three";
import { lerp2, lerp3 } from "./math";


/**
 *
 * @param {Vector3[]} points
 * @param {number} _delta
 * @returns
 */
const getPointAt = (points, _delta) => {

    if (points.length === 1) return points[0];

    const cc = [];

    for (let i = 1; i < points.length; i++) {

        const v = points[i - 1].z !== undefined ? lerp3(points[i - 1], points[i], _delta) : lerp2(points[i - 1], points[i], _delta);

        cc.push(v);

    }

    return getPointAt(cc, _delta);

};

/**
 * @typedef {Vector2|Vector3} Point 
 * @param {Point[]} points
 * @param {number} t
 * @returns {Point[]}
 */
export function bezier3(points, t) {

    if (points.length < 3) {

        return points;

    }

    const results = [points[0]];

    for (let i = 1; i < t; i++) {

        results.push(getPointAt(points, i / t));

    }

    results[results.length - 1] = points[points.length - 1];

    return results;

}
